package com.jun.controller;

import com.jun.entity.Consume;
import com.jun.entity.UserConsume;
import com.jun.service.ConsumeService;
import com.jun.service.UserConsumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("general_user") //普通用户的控制层
@Slf4j
public class GeneralUser {
    @Autowired
    UserConsumeService userConsumeService;
    @Autowired
    ConsumeService consumeService;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @PostMapping("/profitIn")   //普通用户收益录入
    public Map<String,Object> profitIn(@RequestBody Map<String,Object> map){
        HashMap<String, Object> hashMap = new HashMap<>();
        String profitIn = (String) map.get("profitIn"); //得到要录入的收益
        String time = simpleDateFormat.format(new Date());
        int i = userConsumeService.insertUserConsume(new UserConsume("", profitIn, time, "普通用户收益录入"));
        if(i==1){
            hashMap.put("state",true);
            hashMap.put("msg","录入成功！");
            log.info("普通用户收益录入成功！");
            int f = consumeService.insertConsume(new Consume("0", profitIn, time, "普通用户消费"));
            if(f==1){
                log.info("插入一条总消费记录成功！");
            }else{
                log.info("插入一条总消费记录失败！");
            }
        }else{
            hashMap.put("state",false);
            hashMap.put("msg","录入失败！");
            log.info("普通用户收益录入失败！");
        }
        return hashMap;
    }
}
